<?php


namespace App\Utils;


use AlibabaCloud\Client\AlibabaCloud;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Sms\V20190711\SmsClient;

class Sms
{
    // 短信通道 1:腾讯云 2:阿里云
    private $channel = null;

    // appId
    private $appId = null;

    // SecretId
    private $secretId = null;

    // SecretKey
    private $secretKey = null;

    // signature
    private $signature = null;

    /**
     * 初始化方法
     * Sms constructor.
     * @param $channel
     * @param $secretId
     * @param $secretKey
     * @param null $signature
     * @param null $appId
     * @throws \Exception
     */
    public function __construct($channel, $secretId, $secretKey, $signature = null, $appId = null) {
        if (!in_array($channel, [1,2])) {
            throw new \Exception('目前仅支持腾讯或阿里短信通道', Code::LACK_OF_CONDITIONS);
        }
        if ($appId) {
            $this->appId = $appId;
        }
        $this->channel = $channel;
        $this->secretId = $secretId;
        $this->secretKey = $secretKey;
        $this->signature = $signature;
    }

    /**
     * 发送验证码
     * @param $phone
     * @param $code
     * @param $tpl
     * @return \AlibabaCloud\Client\Result\Result|false|string
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     */
    public function captcha($phone, $code, $tpl) {
        switch ($this->channel) {
            case 1:
                $cred = new Credential($this->secretId, $this->secretKey);
                $httpProfile = new HttpProfile();
                $httpProfile->setReqMethod('POST');
                $httpProfile->setEndpoint('sms.tencentcloudapi.com');

                $clientProfile = new ClientProfile();
                $clientProfile->setHttpProfile($httpProfile);

                $client = new SmsClient($cred, "", $clientProfile);
                $req = new SendSmsRequest();
                $req->fromJsonString(json_encode([
                    'PhoneNumberSet' => [ '+86' . $phone ],
                    'TemplateID' => $tpl,
                    'Sign' => $this->signature,
                    'TemplateParamSet' => [ $code ],
                    'SmsSdkAppid' => $this->appId
                ], JSON_UNESCAPED_UNICODE));
                return ($client->SendSms($req))->toJsonString();
            case 2:
                AlibabaCloud::accessKeyClient($this->secretId, $this->secretKey)
                    ->regionId('cn-hangzhou')->asDefaultClient();
                return AlibabaCloud::rpc()->product('Dysmsapi')->version('2017-05-25')
                    ->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options([
                        'query' => [
                            'RegionId' => "cn-hangzhou",
                            'PhoneNumbers' => $phone,
                            'SignName' => $this->signature,
                            'TemplateCode' => $tpl,
                            'TemplateParam' => json_encode(['code' => $code], JSON_UNESCAPED_UNICODE)
                        ]
                    ])->request();
            default:
                return false;
        }
    }
}
